package com.dqhl.community.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.dqhl.community.R;

/**
 * Created by Administrator on 2016/8/10.
 * 确认 dialog
 */
public class ConfirmDialog extends Dialog implements View.OnClickListener{
    private Context context;
    private String content; // dialog 提示的内容
    private String confirm; // dialog 确定按钮显示内容
    private String cancel;  // dialog 取消按钮显示内容

    private ButtonClickListener buttonClickListener;

    public void setButtonClickListener(ButtonClickListener buttonClickListener) {
        this.buttonClickListener = buttonClickListener;
    }

    public ConfirmDialog(Context context, String content, String confirm, String cancel) {
        super(context, R.style.CustomDialog);
        this.context = context;
        this.content = content;
        this.confirm = confirm;
        this.cancel = cancel;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
    }

    private void initView() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_confirm, null);
        setContentView(view);

        TextView tvContent = (TextView) view.findViewById(R.id.tv_content);
        Button btnConfirm = (Button) view.findViewById(R.id.btn_confirm);
        Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);

        tvContent.setText(content);
        btnConfirm.setText(confirm);
        btnCancel.setText(cancel);

        btnConfirm.setOnClickListener(this);
        btnCancel.setOnClickListener(this);

        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        lp.width = (int) (dm.widthPixels * 0.8);
        window.setAttributes(lp);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_confirm:
                buttonClickListener.doConfirm();
                break;
            case R.id.btn_cancel:
                buttonClickListener.doCancel();
                break;
        }
    }

    public interface ButtonClickListener {

        void doConfirm();

        void doCancel();
    }
}
